Work and Energy


import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML

Tutorial Problem 3.1

Use work-energy theorem on projectile motion

def v(v0, t, g=9.81):
    return v0 - g*t 

def x(x0, v0, t, g=9.81):
    return x0 + v0*t - g*t**2/2 

v0 = 200 # initial velocity
g = 9.81 #m/s2
x0 = 0
m = 100 #kg

time = np.arange(0, 40.8, 0.1)

velocity = v(v0, time)

h = x(x0, v0, time) # height

ke = 0.5 * m * velocity**2 # kinetic energy

gpe = m * g * h # gravitational potential energy

print("Speed of rock when it leaves the ground = %.f m/s" % v0)

print("Speed of rock when it reaches the ground = %.3f m/s" % (velocity[-1]))

if np.allclose(abs(velocity[0]), abs(velocity[-1]), 1) == True: # error within 1m/s
    print("Speed at which the rock leaves the ground is equal to that when it reaches the ground")
else:
    print("Error")
Speed of rock when it leaves the ground = 200 m/s
Speed of rock when it reaches the ground = -199.267 m/s
Speed at which the rock leaves the ground is equal to that when it reaches the ground
nframes = len(time)


# Plot background axes
fig, axes = plt.subplots(1,3, figsize=(18,10))

# define lines
line1, = axes[0].plot([], [], 'ko', lw=2)
line2, = axes[1].plot([], [], 'ro', lw=2)
line3, = axes[2].plot([], [], 'bo', lw=2)


# customise axes 

axes[0].set_xlim((-0.5, 0.5))
axes[0].set_ylim((0, 2040))
axes[0].set_ylabel('height (m)')
axes[0].set_title('Trajectory of rock')


axes[1].set_xlim((-0.5, 0.5))
axes[1].set_ylim((0, 2000000))
axes[1].set_title('KE of rock over time fast forward x10')


axes[2].set_xlim((-0.5, 0.5))
axes[2].set_ylim((0, 2000000))
axes[2].set_title('GPE of rock over time fast forward x10')

    
lines = [line1, line2, line3]

plt.subplots_adjust(hspace=0.5)

# Plot background for each frame
def init():
    for line in lines:
        line.set_data([], [])
    return lines

# Set what data to plot in each frame
def animate(i):
    
    x1 = 0
    y1 = h[i]
    lines[0].set_data(x1, y1)
    
    x2 = 0
    y2 = ke[i]
    lines[1].set_data(x2, y2)

    
    x3 = 0
    y3 = gpe[i]
    lines[2].set_data(x3, y3)
    
    return lines

# Call the animator
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=nframes, interval=10, blit=True)
../../_images/3_Work_and_Energy_4_0.png
HTML(anim.to_jshtml())

Tutorial Problem 3.2, 3.6

Use work-energy theorem on mass attached to spring hanging vertically

Equipartition of energy

m = 10 # mass kg
k = 1e3 # spring constant N/m
g = 9.81 # m/s2

d = 2 * m * g / k

print("Maximum distance the mass will drop is %.5f m" % d)

T = 2 * np.pi * np.sqrt(m/k) # simple harmonic motion

t = np.linspace(0, T, 500)

y = d * np.sin(np.sqrt(k/m)*t)

dydt = d * np.sqrt(k/m) * np.cos(np.sqrt(k/m)*t)

gpe = m * g * y

epe = 0.5 * k * y**2

ke = 0.5 * m * dydt**2

average_epe = np.sum(epe)/len(epe)

average_ke = np.sum(ke)/len(ke)

average_gpe = np.sum(gpe)/len(gpe)
Maximum distance the mass will drop is 0.19620 m
nframes = len(t)


# Plot background axes
fig, axes = plt.subplots(1,4, figsize=(18,10))

# define lines
line1, = axes[0].plot([], [], 'ko', lw=2)
line2, = axes[1].plot([], [], 'ro', lw=2)
line3, = axes[2].plot([], [], 'go', lw=2)
line4, = axes[3].plot([], [], 'bo', lw=2)


# customise axes 

axes[0].set_xlim((-0.5, 0.5))
axes[0].set_ylim((-0.2, 0.2))
axes[0].set_ylabel('height (m)')
axes[0].set_title('Trajectory of mass')


axes[1].set_xlim((-0.5, 0.5))
axes[1].set_ylim((-20, 20))
axes[1].set_title('GPE of mass (J)')


axes[2].set_xlim((-0.5, 0.5))
axes[2].set_ylim((0, 20))
axes[2].set_title('EPE of mass (J)')

axes[3].set_xlim((-0.5, 0.5))
axes[3].set_ylim((0, 20))
axes[3].set_title('KE of mass (J)')
    
lines = [line1, line2, line3, line4]

plt.subplots_adjust(hspace=0.5)

# Plot background for each frame
def init():
    for line in lines:
        line.set_data([], [])
    return lines

# Set what data to plot in each frame
def animate(i):
    
    x1 = 0
    y1 = y[i]
    lines[0].set_data(x1, y1)
    
    x2 = 0
    y2 = gpe[i]
    lines[1].set_data(x2, y2)

    
    x3 = 0
    y3 = epe[i]
    lines[2].set_data(x3, y3)
    
    x4 = 0
    y4 = ke[i]
    lines[3].set_data(x4, y4)
    
    return lines

# Call the animator
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=nframes, interval=10, blit=True)
../../_images/3_Work_and_Energy_8_0.png
HTML(anim.to_html5_video())
if np.allclose(average_ke, average_epe, 1e4) == True:
    print("Over 1 period, average KE = average EPE = %.2f J" % (average_ke))
else:
    print("Error")
    
print("Average GPE = %.f J" % (average_gpe))

print("Total average energy = %.2f J" % (average_ke + average_epe + average_gpe))
Over 1 period, average KE = average EPE = 9.64 J
Average GPE = 0 J
Total average energy = 19.25 J